local skel = fk.CreateSkill {
  name = "lb__yanyun",
}

Fk:loadTranslationTable {
  ["lb__yanyun"] = "烟煴",
  [":lb__yanyun"] = "每回合首张被使用的♣牌结算后，你可以秘密选择包含使用者的任意名座次相连的角色，令其同时选择是否弃置一张牌，然后你可以令其中因此弃牌的角色各自摸两张牌。",

  ["#lb__yanyun"] = "烟煴：你可以秘密选择包含 %src 的任意名座次相连的角色，令这些角色同时选择是否弃置一张牌，然后你可以令因此弃牌的角色各自摸两张牌",
  ["#lb__yanyun-dis"] = "烟煴：你可以弃置一张牌",
  ["#lb__yanyun-draw"] = "烟煴：你可以令因〖烟煴〗弃牌的角色各自摸两张牌",

  ["$lb__yanyun1"] = "炉香袅孤碧，云缕霏数千。",
  ["$lb__yanyun2"] = "金鳞燃犀，洞若观火。",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and data.card.suit == Card.Club and target:isAlive() then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data
        return use.card.suit == Card.Club
      end, Player.HistoryTurn)
      return #use_events == 1 and use_events[1].data == data
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local s, ret = room:askToUseActiveSkill(player, {
      skill_name = "#lb__yanyun-choose",
      cancelable = true,
      prompt = "#lb__yanyun:" .. target.id,
      extra_data = { extra_data = { id = target.id } }
    })
    if s and ret and #ret.targets > 0 then
      event:setCostData(self, { targets = ret.targets })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).targets
    room:sortByAction(tos)
    local extra_data = {
      num = 1,
      min_num = 1,
      include_equip = true,
      skillName = skel.name,
      pattern = ".",
    }
    local dat = {
      "discard_skill",
      "#lb__yanyun-dis",
      true,
      extra_data,
    }
    local req = Request:new(tos, "AskForUseActiveSkill")
    req.focus_players = room.alive_players
    req.focus_text = skel.name
    req.n = #tos
    for _, p in ipairs(tos) do req:setData(p, dat) end
    req:ask()
    local dis_tos = {}
    for _, to in ipairs(room:getAlivePlayers()) do
      if table.contains(req.winners, to) and to:isAlive() then
        local result = req:getResult(to)
        local ids = result
        if result ~= "" then
          if result.card then
            ids = result.card.subcards
          else
            ids = result
          end
        end
        if table.every(Card:getIdList(ids), function(id)
              return room:getCardOwner(id) == to
            end) then
          room:throwCard(ids, skel.name, to, to)
          table.insert(dis_tos, to)
        end
      end
    end
    dis_tos = table.filter(dis_tos, function (to)
      return to:isAlive()
    end)
    if #dis_tos > 0 and player:isAlive() and room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__yanyun-draw" }) then
      room:sortByAction(dis_tos)
      for _, to in ipairs(dis_tos) do
        if to:isAlive() then
          to:drawCards(2, skel.name)
        end
      end
    end
  end,
})

return skel
